import { defineStore } from 'pinia'
import $kt from '@/vueKantboot'

type BreadcrumbType = [{
    name: string,
    path: string,
    code: string
}]


// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const useStore = defineStore('main', {
    // other options...

    state: () => ({
        breadcrumb: [] as unknown as BreadcrumbType,
        languageCode: "",
        appFront: {
            "account": "账号",
            "addDirectory": "添加目录",
            "avatar": "头像",
            "avatarSelectTitle": "头像选择",
            "beta": "测试版",
            "chat": "聊天",
            "chatValue": "对话内容",
            "contentNotNull": "内容不能空",
            "description": "描述",
            "descriptionNotNull": "描述不能为空",
            "email": "邮箱",
            "gptChatGenerating": "正在生成回复",
            "historicRecords": "历史记录",
            "homePage": "首页",
            "languageSelect": "语言选择",
            "login": "登录",
            "my": "我的",
            "noOpen": "暂未开放",
            "oneClickLogin": "一键登录",
            "party": "派对",
            "philosophicalAssistant": "哲学助手",
            "phone": "手机号码",
            "register": "注册",
            "sending": "正在发送",
            "sent": "发送",
            "switchLanguage": "切换语言",
            "title": "标题",
            "titleNotNull": "标题不能为空",
            "password": "密码",
            "otherLoginType": "其他登录方式",
            "forgotPassword": "忘记密码",
            "obituary": "生死簿",
            "managementSystem": "管理系统",
            "slogan": "生与死，是一种状态，而不是一种结果。",
            "search": "搜索",
            "nickname": "昵称",
            "role": "角色",
            "logOut": "退出登录",
            "delete": "刪除",
            "edit": "编辑",
            "numberOfEntries": "条目数",
            "noData": "没有数据",
            "all": "全部",
            "add": "添加",
            "addGroup": "添加分组",
            "serialNumber": "序号",
            "createTime": "创建时间",
            "startTime": "开始时间",
            "endTime": "结束时间",
            "operate": "操作",
            "deletePrompt": "是否确认删除?",
            "cancel": "取消",
            "confirm": "确定",
        }
    }),

    actions: {
        setLanguageCode(languageCode: string) {
            localStorage.setItem('languageCode', languageCode);
            this.languageCode = languageCode;
            $kt.request.request({
                url: '/system/dictI18n/getAppFront',
                method: 'post'
            }).then((res: any) => {
                this.appFront = res.data.data;
            });
        },
        /**
         * 选择面包屑
         * @param item 
         */
        selectBreadcrumb(item: any) {
            localStorage.setItem('breadcrumb', JSON.stringify(this.breadcrumb));
            for (let i = 0; i < this.breadcrumb.length; i++) {
                if (this.breadcrumb[i].code == item.code) {
                    this.breadcrumb.splice(i, 1);
                    break;
                }
            }
            this.breadcrumb.push(item);
            if (this.breadcrumb.length > 5) {
                // 保持最多5个,超过5个删除第一个
                this.breadcrumb.shift();
            }
        },
        /**
         * 删除面包屑
         * @param item
         * @returns
         * @memberof MainStore
         * @description 删除面包屑
         */
        removeBreadcrumb(item: any) {
            for (let i = 0; i < this.breadcrumb.length; i++) {
                if (this.breadcrumb[i].code == item.code) {
                    this.breadcrumb.splice(i, 1);
                    break;
                }
            }
            localStorage.setItem('breadcrumb', JSON.stringify(this.breadcrumb));
        },
        getLanguageCode() {
            const loanguageCode = localStorage.getItem('languageCode') || navigator.language || "en_US";
            return loanguageCode.replace("_", "-");
        }
    }
})